import TankBot from "./Tank/TankBot";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GamePanel extends cc.Component {

    private initControl() {
        // const collisionManager = cc.director.getCollisionManager();
        // collisionManager.enabled = true;
        // collisionManager.enabledDebugDraw = true;
        // collisionManager.enabledDrawBoundingBox = true;

        const physicsManager = cc.director.getPhysicsManager();
        physicsManager.enabled = true;
        // const drawBits = cc.PhysicsManager.DrawBits as any;
        // physicsManager.debugDrawFlags = drawBits.e_aabbBit |
        //     drawBits.e_pairBit |
        //     drawBits.e_centerOfMassBit |
        //     drawBits.e_jointBit |
        //     drawBits.e_shapeBit;

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        window.addEventListener('load', this.setCanvasFocus);
        this.setCanvasFocus();
        window.document.addEventListener('click', this.setCanvasFocus);
    }
    private onKeyUp(event: cc.Event.EventKeyboard) {
        switch (event.keyCode) {
            case cc.macro.KEY.f5:
                window.location.reload();
                break;

            default:
                break;
        }
    }
    private setCanvasFocus() {
        const canvas = window.document.querySelector('canvas');
        canvas.focus();
    }

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.initControl();
        TankBot.autoCreate();
    }

    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        window.document.removeEventListener('click', this.setCanvasFocus);
    }

    start() {
    }

    // update (dt) {}
}
